#include "stdafx.h"
#include "MsgStructs.h"
#include "ByteBuffer.h"

#define SAFE_READ(X) {if(!X) {return false;}}
#define SAFE_WRITE(X) {if(!X) {return false;}}

bool CTOS_Login::SetValue(CByteBuffer *_pByteBuf)
{
	SAFE_READ(_pByteBuf->ReadString(m_strAccount));
	SAFE_READ(_pByteBuf->ReadString(m_strPwd));
	SAFE_READ(_pByteBuf->ReadInt(m_nMsgIndex));
	SAFE_READ(_pByteBuf->ReadFloat(m_fTime));

	int nItemCount = 0;
	SAFE_READ(_pByteBuf->ReadInt(nItemCount));

	m_vItem.resize(nItemCount);
	for (int i = 0; i < nItemCount; i++)
	{
		SAFE_READ(m_vItem[i].SetValue(_pByteBuf))
	}

	return true;
}

bool CTOS_Item::SetValue(CByteBuffer *_pByteBuf)
{
	SAFE_READ(_pByteBuf->ReadInt(m_nItemID));
	SAFE_READ(_pByteBuf->ReadInt(m_nItemCount));

	return true;
}

bool CTOS_Item::GetByteValue(CByteBuffer *_pByteBuf)
{
	SAFE_WRITE(_pByteBuf->WriteInt(m_nItemID));
	SAFE_WRITE(_pByteBuf->WriteInt(m_nItemCount));
	return true;
}

bool STOC_LOGIN_RET::GetByteValue(CByteBuffer *_pByteBuf)
{
	SAFE_WRITE(_pByteBuf->WriteInt((int)m_eLoginRet));
	SAFE_WRITE(_pByteBuf->WriteString(m_strAccount));
	SAFE_WRITE(_pByteBuf->WriteString(m_strPwd));
	SAFE_WRITE(_pByteBuf->WriteInt(m_nMsgIndex));
	byte nCount = (byte)m_vItem.size();
	SAFE_WRITE(_pByteBuf->WriteByte(nCount));
	for(int i = 0; i < nCount; i++)
	{
		SAFE_WRITE(m_vItem[i].GetByteValue(_pByteBuf));
	}

	return true;
}
